package com.uzai.console.service.mini.impl;

import com.alibaba.fastjson.JSONObject;
import com.uzai.common.dto.base.UzaiRespVo;
import com.uzai.common.enums.mini.MiniOrderServiceStatus;
import com.uzai.common.enums.mini.MiniOrderStatus;
import com.uzai.common.enums.mini.MiniTradeStatus;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.cashier.CashierWxPayClient;
import com.uzai.console.cashier.dto.WxOrderQueryDto;
import com.uzai.console.cashier.enums.TradeStatusEnum;
import com.uzai.console.cashier.param.CashierWechatPayQueryParam;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.entity.MiniOrder;
import com.uzai.console.entity.MiniOrderService;
import com.uzai.console.entity.MiniTrade;
import com.uzai.console.enums.AlipayStatusEnum;
import com.uzai.console.mapper.MiniOrderMapper;
import com.uzai.console.mapper.MiniOrderServiceMapper;
import com.uzai.console.mapper.MiniTradeMapper;
import com.uzai.console.service.mini.MiniTradeService;
import com.uzai.console.service.ratelimit.RatelimitService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * @author liuqi
 * @date 2021年08月25日 20:17
 */
@Service
public class MiniTradeServiceImpl implements MiniTradeService {

    Logger logger = LoggerFactory.getLogger(MiniTradeServiceImpl.class);

    @Autowired
    private MiniTradeMapper miniTradeMapper;
    @Autowired
    private RatelimitService ratelimitService;
    @Autowired
    private CashierWxPayClient cashierWxPayClient;
    @Autowired
    private MiniOrderServiceMapper miniOrderServiceMapper;
    @Autowired
    private MiniOrderMapper miniOrderMapper;

    /**
     * 小程序交易查询
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param
     * @return null
     */
    public void miniTradeQueryState(MiniTrade miniTrade){

        int now = DateUtil.getNowTime();

        if(miniTrade != null){
            //交易中
            if(miniTrade.getStatus() == 1){
                if(now - miniTrade.getCreateTime().intValue() < 1*3600 ) { //等待时间1小时之内的订单
                    CashierWechatPayQueryParam param = new CashierWechatPayQueryParam();
                    param.setTradeNo(miniTrade.getSourceOutsideCode());
                    param.setRefundTradeNo(miniTrade.getOutsideCode());
                    UzaiRespVo<WxOrderQueryDto> uzaiRespVo = cashierWxPayClient.queryRefundOrder(param);
                    logger.info("--调用小程序查询退款状态接口,para={}, result={}", JSONObject.toJSONString(param), JSONObject.toJSONString(uzaiRespVo));
                    if(uzaiRespVo != null && uzaiRespVo.getCode() == 200) {
                        WxOrderQueryDto orderQueryDto = uzaiRespVo.getData();
                        if (orderQueryDto != null) {
                            //支付成功
                            if (TradeStatusEnum.SUCCESS.equals(orderQueryDto.getStatus())) {
                                miniTrade.setStatus(MiniTradeStatus.TRADE_SUCCESS.getCode());
                                miniTrade.setOutsideStatus(1);
                                miniTrade.setFinishTime(now);
                                miniTrade.setUpdateTime(now);
                                miniTradeMapper.updateStatus(miniTrade);

                                //修改维权售后
                                MiniOrderService miniOrderService = miniOrderServiceMapper.selectById(miniTrade.getServiceId(),miniTrade.getMerId());
                                if(miniOrderService != null){
                                    miniOrderService.setStatus(MiniOrderServiceStatus.REFUND_SUCCESS.getCode());
                                    miniOrderService.setUpdateTime(now);
                                    miniOrderServiceMapper.updateStatus(miniOrderService);
                                }

                                //批量查询此父订单号订单
                                MiniOrder miniOrder = miniOrderMapper.selectById(miniOrderService.getOrderId(), miniOrderService.getMerId());
                                if(miniOrder != null){
                                    miniOrder.setStatus(MiniOrderStatus.REFUND.getCode());
                                    miniOrder.setUpdateTime(now);
                                    miniOrderMapper.updateStatus(miniOrder);
                                }
                            } else if (TradeStatusEnum.ABNORMAL.equals(orderQueryDto.getStatus())) {
                                miniTrade.setStatus(MiniTradeStatus.TRADE_FAIL.getCode());
                                miniTrade.setOutsideStatus(0);
                                miniTrade.setUpdateTime(now);
                                miniTradeMapper.updateStatus(miniTrade);

                                //修改维权售后
                                MiniOrderService miniOrderService = miniOrderServiceMapper.selectById(miniTrade.getServiceId(),miniTrade.getMerId());
                                if(miniOrderService != null){
                                    miniOrderService.setStatus(MiniOrderServiceStatus.REFUND_FAIL.getCode());
                                    miniOrderService.setUpdateTime(now);
                                    miniOrderServiceMapper.updateStatus(miniOrderService);
                                }

                            } else if (TradeStatusEnum.CLOSED.equals(orderQueryDto.getStatus())) {
                                miniTrade.setStatus(MiniTradeStatus.TRADE_CANCEL.getCode());
                                miniTrade.setOutsideStatus(0);
                                miniTrade.setUpdateTime(now);
                                miniTradeMapper.updateStatus(miniTrade);

                                //修改维权售后
                                MiniOrderService miniOrderService = miniOrderServiceMapper.selectById(miniTrade.getServiceId(),miniTrade.getMerId());
                                if(miniOrderService != null){
                                    miniOrderService.setStatus(MiniOrderServiceStatus.REFUND_FAIL.getCode());
                                    miniOrderService.setUpdateTime(now);
                                    miniOrderServiceMapper.updateStatus(miniOrderService);
                                }
                            }
                        }
                    }
                }else{
                    miniTrade.setStatus(MiniTradeStatus.TRADE_CANCEL.getCode());
                    miniTrade.setOutsideStatus(0);
                    miniTrade.setUpdateTime(now);
                    miniTradeMapper.updateStatus(miniTrade);

                    //修改维权售后
                    MiniOrderService miniOrderService = miniOrderServiceMapper.selectById(miniTrade.getServiceId(),miniTrade.getMerId());
                    if(miniOrderService != null){
                        miniOrderService.setStatus(MiniOrderServiceStatus.REFUND_FAIL.getCode());
                        miniOrderService.setUpdateTime(now);
                        miniOrderServiceMapper.updateStatus(miniOrderService);
                    }

                }
            }
        }

    }

}
